const Koa = require('koa');
const next = require('next');
const static = require('koa-static');
const path = require('path');
const dev = process.env.NODE_ENV === 'development';
const app = next({ dev });

const router = require('./node/routes')(app);

const server = new Koa();

const port = process.env.PORT || 6020;

app.prepare().then(() => {
  // 配置静态资源
  const staticPath = './public';
  server.use(static(path.join(__dirname, staticPath)));

  server.use(async (ctx, nxt) => {
    ctx.res.statusCode = 200;
    await nxt();
  });

  server.use(router.routes());

  server.on('error', (err, ctx) => {
    Sentry.withScope((scope) => {
      scope.addEventProcessor((event) => {
        return Sentry.Handlers.parseRequest(event, ctx.request);
      });
      Sentry.captureException(err);
    });
  });

  server.listen(port, () => {
    console.log(`> Ready on http://localhost:${port}`);
  });
});
